package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Employee;
import cn.wolfcode.crm.query.EmployeeQueryObject;
import cn.wolfcode.crm.service.IDepartmentService;
import cn.wolfcode.crm.service.IEmployeeService;
import cn.wolfcode.crm.service.IRoleService;
import cn.wolfcode.crm.util.JsonResult;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Controller
@RequestMapping("employee")
public class EmployeeController {
    @Autowired
    private IEmployeeService employeeService;
    @Autowired
    private IDepartmentService departmentService;
    @Autowired
    private IRoleService roleService;

    @RequiresPermissions(value = {"员工列表","employee:list"},logical = Logical.OR)
    @RequestMapping("list")
    public String list(@ModelAttribute("qo") EmployeeQueryObject qo, Model model) {
        model.addAttribute("pageInfo", employeeService.query(qo));
        model.addAttribute("depts", departmentService.list());
        return "employee/list";
    }

    @RequiresPermissions(value = {"编辑员工","employee:input"},logical = Logical.OR)
    @RequestMapping("input")
    public String input(Long id, Model model) {
        if (id != null) {
            model.addAttribute("entity", employeeService.get(id));
        }
        model.addAttribute("depts", departmentService.list());
        model.addAttribute("roles",roleService.list());
        return "employee/input";
    }

    @RequiresPermissions(value = {"更新/保存员工","employee:saveOrUpdate"},logical = Logical.OR)
    @RequestMapping("saveOrUpdate")
    public String saveOrUpdate(Employee entity,Long[] ids) {
        employeeService.saveOrUpdate(entity, ids);
        return "redirect:/employee/list.do";
    }

    @RequiresPermissions(value = {"删除员工","employee:delete"},logical = Logical.OR)
    @RequestMapping("delete")
    @ResponseBody
    public Object delete(Long id) {
        if (id != null) {
            employeeService.delete(id);
        }
        return new JsonResult();
    }

    @RequestMapping("batchDelete")
    @ResponseBody
    public Object batchDelete(Long[] ids) {
        if (ids!=null) {
            employeeService.batchDelete(ids);
        }
        return new JsonResult();
    }


    @RequestMapping("checkName")
    @ResponseBody
    public boolean checkName(String name) {
        return employeeService.checkName(name);
    }

    @RequestMapping("export")
    public ModelAndView export(HttpServletResponse response) throws Exception {
        Workbook wb = new HSSFWorkbook();
        Sheet sheet = wb.createSheet("员工列表");
        Row row = sheet.createRow(0);
        row.createCell(0).setCellValue("姓名");
        row.createCell(1).setCellValue("邮箱");
        row.createCell(2,CellType.NUMERIC).setCellValue("年龄");
        List<Employee> list = employeeService.selectAll();
        for (int i = 1; i <= list.size(); i++) {
            Employee employee = list.get(i-1);
            row = sheet.createRow(i);
            row.createCell(0).setCellValue(employee.getName());
            row.createCell(1).setCellValue(employee.getEmail());
            row.createCell(2,CellType.NUMERIC).setCellValue(employee.getAge());
        }
        //设置下载文件的名称
        response.setHeader("Content-Disposition","attachment;filename=employee.xlsx");
        wb.write(response.getOutputStream());
        return null;
    }

    @RequestMapping("importEmployee")
    public String importEmployee(MultipartFile xls) throws Exception {
        if (xls!=null&&"application/vnd.ms-excel".equals(xls.getContentType())){
            Workbook wb = new HSSFWorkbook(xls.getInputStream());
            Sheet sheet = wb.getSheetAt(0);
            int lastRowNum = sheet.getLastRowNum();
            for (int i = 1; i <= lastRowNum; i++) {
                Row row = sheet.getRow(i);
                String name = row.getCell(0).getStringCellValue();
                String email = row.getCell(1).getStringCellValue();
                Double age = row.getCell(2).getNumericCellValue();
                if (employeeService.checkName(name)){
                    Employee employee = new Employee();
                    employee.setPassword("1");
                    employee.setName(name);
                    employee.setEmail(email);
                    employee.setAge(age.intValue());
                    employeeService.saveOrUpdate(employee,null);
                }
            }
        }
        return "redirect:/employee/list.do";
    }
}
